package com.vcredit.creditcat.service.product.h5;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.util.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.core.type.TypeReference;
import com.vcredit.bird.enumtype.BankEnum;
import com.vcredit.creditcat.dto.CustCardDto;
import com.vcredit.creditcat.dto.product.h5.BindBankCardParamDto;
import com.vcredit.creditcat.dto.product.h5.CheckOrderResultDto;
import com.vcredit.creditcat.dto.product.h5.ContractDto;
import com.vcredit.creditcat.dto.product.h5.CreateOrderParamDto;
import com.vcredit.creditcat.dto.product.h5.LoanBindCardDto;
import com.vcredit.creditcat.dto.product.h5.LoanBindCardListDto;
import com.vcredit.creditcat.dto.product.h5.LoanOrderIndexDto;
import com.vcredit.creditcat.dto.product.h5.QueryBankByCardResultDto;
import com.vcredit.creditcat.dto.product.h5.QueryContractResultDto;
import com.vcredit.creditcat.dto.product.h5.QueryOrderStatusResultDto;
import com.vcredit.creditcat.dto.product.h5.SubmitBindCardParamDto;
import com.vcredit.creditcat.dto.product.h5.TryCalculateParamDto;
import com.vcredit.creditcat.dto.product.h5.TryCalculateResultDto;
import com.vcredit.creditcat.dto.resource.ResourceDto;
import com.vcredit.creditcat.enumtype.BankCardTypeEnum;
import com.vcredit.creditcat.enumtype.LoanTermUnitEnum;
import com.vcredit.creditcat.enumtype.ProductApiEnum;
import com.vcredit.creditcat.enumtype.ProductAuditResultEnum;
import com.vcredit.creditcat.enumtype.ResourceTypeEnum;
import com.vcredit.creditcat.enumtype.product.h5.LoanOrderStatusEnum;
import com.vcredit.creditcat.exception.CustCardExcetpion;
import com.vcredit.creditcat.model.AuthIdentity;
import com.vcredit.creditcat.model.BankInfo;
import com.vcredit.creditcat.model.Cust;
import com.vcredit.creditcat.model.CustProductApply;
import com.vcredit.creditcat.model.ProcessSpecial;
import com.vcredit.creditcat.model.ProductApiUrl;
import com.vcredit.creditcat.model.ProductLoanBindCard;
import com.vcredit.creditcat.model.ProductOrderLog;
import com.vcredit.creditcat.service.AuthIdentityService;
import com.vcredit.creditcat.service.AuthMobileService;
import com.vcredit.creditcat.service.BankInfoService;
import com.vcredit.creditcat.service.CustAccountService;
import com.vcredit.creditcat.service.CustCardService;
import com.vcredit.creditcat.service.CustProductApplyService;
import com.vcredit.creditcat.service.ProductApiUrlService;
import com.vcredit.creditcat.service.ProductLoanBindCardService;
import com.vcredit.creditcat.service.ProductOrderLogService;
import com.vcredit.creditcat.service.resource.FtpResourceService;
import com.vcredit.creditcat.service.speedloan.ProcessSpecialService;
import com.vcredit.creditcat.util.Utility;
import com.vcredit.creditcat.vo.product.h5.CommonResultVo;
import com.vcredit.creditcat.vo.product.h5.QueryContractResultDataVo;
import com.vcredit.creditcat.vo.product.h5.TryCalculateResultDataVo;
import com.vcredit.framework.annotation.Log;
import com.vcredit.framework.exception.BaseException;
import com.vcredit.framework.util.JacksonUtil;
import com.vcredit.framework.util.MD5Util;

@Service
public class AfterCreditService implements IAfterCreditService {
	
	private static final Logger LOG = LoggerFactory.getLogger(AfterCreditService.class);
	
	public static final String CUSTID_PREFIX = "creditcat_";
	public static final String DAI_KUAN_XIE_YI = "贷款协议";
	
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	
	@Autowired
	private ProductApiUrlService productApiUrlService;
	@Autowired
	private AuthIdentityService authIdentityService;
	@Autowired
	private AuthMobileService authMobileService;
	@Autowired
	private CustCardService custCardService;
	@Autowired
	private CustProductApplyService custProductApplyService;
	@Autowired
	private ProductOrderLogService productOrderLogService;
	@Autowired
	private ProductLoanBindCardService productLoanBindCardService;
	@Autowired
	private BankInfoService bankInfoService;
	@Autowired
	private CommonApiRequestService commonApiRequestService;
	@Autowired
	private ProcessSpecialService processSpecialService;
	@Autowired
	private FtpResourceService ftpResourceService;
	@Autowired
	private SdAfterCreditService sdAfterCreditService;
	@Autowired
	private CustAccountService custAccountService;

	private String getUrlByProductAndType(Long productId, Integer type) {
		Map<Long, Map<Integer, ProductApiUrl>> findAllForMap = productApiUrlService.findAllForMap();
		Map<Integer, ProductApiUrl> map = findAllForMap.get(productId.longValue());
		return map.get(type.intValue()).getApiUrl();
	}
	
	/**
	 * 交单校验
	 * @author zhaoqi01
	 * @param productId
	 * @param cust
	 * @return
	 * @throws Exception
	 */
	@Log  
	@Override
	public CheckOrderResultDto checkUser(Long productId, Cust cust)
			throws Exception {
		AuthIdentity authIdentity = authIdentityService.findByProcessId(cust.getLastProccessId());
		//AuthMobile authMobile = authMobileService.findByProcessIdAndType(cust.getLastProccessId(), 0);
		String mobile = custAccountService.findByCustId(cust.getCustId()).getUserName();
		String citizenId = authIdentity.getCitizenId();
		
		String citizenIdEnd = citizenId.substring(citizenId.length() - 4);
		String mobileEnd = mobile.substring(mobile.length() - 3);
		
		Map<String,String> paramMap = new HashMap<String,String>();
		paramMap.put("user_idcard", citizenIdEnd);
		paramMap.put("user_idcard_md5", MD5Util.md5(citizenId));
		paramMap.put("user_mobile", mobileEnd);
		paramMap.put("user_mobile_md5", MD5Util.md5(mobile));
		
		String paramJson = JacksonUtil.Obj2Json(paramMap);
		String url=this.getUrlByProductAndType(productId, ProductApiEnum.CHECK_USER.getType());
		LOG.info("------------AfterCreditService.checkOrder入参：" + paramJson+"#"+url);		
		String resultJson = commonApiRequestService.wrapRequest(url, paramJson, false);
		LOG.info("------------AfterCreditService.checkOrder出参：" + resultJson);
		CommonResultVo<Map<String, String>> commonResult = JacksonUtil.Json2Obj(resultJson, new TypeReference<CommonResultVo<Map<String,String>>>(){});
		if(commonResult.isSuccess() && "1".equals(commonResult.getData().get("result"))) {
			return new CheckOrderResultDto(true, null);
		} else {
			return new CheckOrderResultDto(false, commonResult.getMsg());
		}
	}

	@Log
	@Override
	public TryCalculateResultDto tryCalculate(Long productId,
			TryCalculateParamDto param, Cust cust) throws Exception {
		Integer unitType = LoanTermUnitEnum.getTypeByName(param.getUnit());
		if(null == unitType) {
			throw new BaseException("参数错误");
		}
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("user_id", CUSTID_PREFIX + cust.getCustId());
		paramMap.put("amount", Float.valueOf(param.getAmount() + ""));
		paramMap.put("peroid", param.getPeroid());
		paramMap.put("period_type", unitType);
		String paramJson = JacksonUtil.Obj2Json(paramMap);
		LOG.info("------------AfterCreditService.tryCalculate入参：" + paramJson);
		String resultJson = commonApiRequestService.wrapRequest(this.getUrlByProductAndType(productId, ProductApiEnum.TRY_CALCULATE.getType()), paramJson, false);
		LOG.info("------------AfterCreditService.tryCalculate出参：" + resultJson);
		
		CommonResultVo<TryCalculateResultDataVo> commonResult = JacksonUtil.Json2Obj(resultJson, new TypeReference<CommonResultVo<TryCalculateResultDataVo>>(){});
		if(!commonResult.isSuccess()) {
			throw new BaseException(commonResult.getMsg());
		}
		
		TryCalculateResultDataVo data = commonResult.getData();
		
		return new TryCalculateResultDto(data.getService_fee(), data.getPeriod_amount(),
				data.getFee_desc());
	}

	@Log
	@Override
	public String submitLoan(Long productId, CreateOrderParamDto param,
			Cust cust) throws Exception {
		CustProductApply custProductApply = custProductApplyService.findByCustAndProduct(cust.getCustId(), productId);
		if(null == custProductApply || custProductApply.getLoanTermType() == 0) {
			throw new BaseException("未获取额度");
		}
		Map<String,Object> paramMap = new HashMap<String, Object>();
		paramMap.put("user_id", CUSTID_PREFIX + cust.getCustId());
		paramMap.put("loan_amount", param.getLoanAmount());
		paramMap.put("loan_term", param.getLoanPeriod());
		paramMap.put("loan_term_type", custProductApply.getLoanTermType());
		String paramJson = JacksonUtil.Obj2Json(paramMap);
		LOG.info("------------AfterCreditService.submitLoan入参：" + paramJson);
		String resultJson = commonApiRequestService.wrapRequest(this.getUrlByProductAndType(productId, ProductApiEnum.SUBMIT_ORDER.getType()), paramJson, false);
		LOG.info("------------AfterCreditService.submitLoan出参：" + resultJson);
		
		CommonResultVo<Map<String,String>> commonResult = JacksonUtil.Json2Obj(resultJson, new TypeReference<CommonResultVo<Map<String,String>>>(){});
		if(!commonResult.isSuccess()) {
			throw new BaseException(commonResult.getMsg());
		}
		String orderId = commonResult.getData().get("order_id");
		
		// 保存orderId
		custProductApply.setOrderId(orderId);
		custProductApply.setAuditResult(LoanOrderStatusEnum.LoanApplying.getAuditResult());
		custProductApplyService.update(custProductApply);
		
		// 保存订单记录
		ProductOrderLog productOrderLog = new ProductOrderLog();
		productOrderLog.setCustId(cust.getCustId());
		productOrderLog.setProductId(productId);
		productOrderLog.setOrderId(orderId);
		productOrderLog.setLoanAmount(param.getLoanAmount().intValue());
		productOrderLog.setLoanTerm(param.getLoanPeriod());
		productOrderLog.setBankCard(param.getBankCardId());
		productOrderLog.setBankCode(param.getBankCode());
		productOrderLog.setBankName(BankEnum.getDescByCode(param.getBankCode()));
		productOrderLogService.insert(productOrderLog);
		
		return orderId;
	}

	@Log
	@Override
	@Transactional
	public void bindCard(Long productId, BindBankCardParamDto param, Cust cust)
			throws Exception {
		// 信用猫绑定银行卡
		this.bindCustCard(param, cust);
		
		ProductLoanBindCard productLoanBindCard = new ProductLoanBindCard();
		productLoanBindCard.setCustId(cust.getCustId());
		productLoanBindCard.setProductId(productId);
		productLoanBindCard.setBankCard(param.getBankCard());
		List<ProductLoanBindCard> list = productLoanBindCardService.find(productLoanBindCard);
		if(Utility.isNullOrEmpty(list)) {
			// 插入第三方绑卡记录，状态为未绑定成功
			productLoanBindCard.setBankCode(param.getBankCode());
			productLoanBindCard.setBankName(BankEnum.getDescByCode(param.getBankCode()));
			productLoanBindCard.setStatus(0);
			productLoanBindCard.setMobile(param.getMobile());
			
			productLoanBindCardService.insert(productLoanBindCard);
		}
		
		else { // 有该卡的绑定记录
			productLoanBindCard = list.get(0);
			if(productLoanBindCard.getStatus() == 1) { // 如果已绑定成功，不能再次绑定
				throw new BaseException("该卡已绑定");
			}
			productLoanBindCard.setMobile(param.getMobile());
			productLoanBindCardService.update(productLoanBindCard);
		}
		
	}

	@Log
	public CustCardDto bindCustCard(BindBankCardParamDto param, Cust cust)
			throws Exception {
		// 查询卡bin是否为借记卡
		CustCardDto custCardDto = custCardService.queryCardBin(param.getBankCard());
		if (!BankCardTypeEnum.DEBIT.equals(custCardDto.getCardType())) {
			throw new CustCardExcetpion("请输入一张借记卡");
		}
		
		// 验卡
		Boolean flag = custCardService
				.checkCustCard(param.getName(), param.getIdNo(), param.getBankCard(),
						param.getMobile(), param.getBankCode());
		if (!flag) {
			throw new CustCardExcetpion("绑卡失败，请重新尝试");
		} else {
			CustCardDto cardDto =
		            new CustCardDto(0l, param.getBankCode(), "", custCardDto.getCardType(),
		                param.getBankCard(), param.getName(), false, param.getMobile());
	        // 绑卡
	        cust.setCustName(param.getName());
	        cust.setCustIdno(param.getIdNo());
	        cardDto = custCardService.bindCustCardAndItemDone(cust, cardDto);
	        return cardDto;
		}
	}

	@Log
	public QueryContractResultDto queryContract(Long productId, Cust cust) throws Exception {
		Map<String,String> paramMap = new HashMap<String,String>();
		paramMap.put("user_id", CUSTID_PREFIX + cust.getCustId());
		String paramJson = JacksonUtil.Obj2Json(paramMap);
		LOG.info("------------AfterCreditService.queryContract入参：" + paramJson);
		String resultJson = commonApiRequestService.wrapRequest(this.getUrlByProductAndType(productId, ProductApiEnum.QUERY_CONTRACT.getType()), paramJson, false);
		LOG.info("------------AfterCreditService.queryContract出参：" + resultJson);
		
		CommonResultVo<QueryContractResultDataVo> commonResult = JacksonUtil.Json2Obj(resultJson, new TypeReference<CommonResultVo<QueryContractResultDataVo>>(){});
		if(!commonResult.isSuccess()) {
			throw new BaseException(commonResult.getMsg());
		}
		
		ProcessSpecial processSpecial = processSpecialService.findByProductId(productId);
		String contractName = processSpecial.getSpecialName() + DAI_KUAN_XIE_YI;
		QueryContractResultDataVo data = commonResult.getData();
		return new QueryContractResultDto(new ContractDto(contractName, data.getContract_url_1()),
				new ContractDto(contractName, data.getContract_url_2()),
				new ContractDto(contractName, data.getContract_url_3()),
				new ContractDto(contractName, data.getContract_url_4()));
	}

	@Log
	public QueryOrderStatusResultDto queryOrderStatus(Long productId, Cust cust) throws Exception {
		CustProductApply custProductApply = custProductApplyService.findByCustAndProduct(cust.getCustId(), productId);
		
		Map<String,String> paramMap = new HashMap<String,String>();
		paramMap.put("user_id", CUSTID_PREFIX + cust.getCustId());
		paramMap.put("order_id", custProductApply.getOrderId());
		
		String paramJson = JacksonUtil.Obj2Json(paramMap);
		LOG.info("------------AfterCreditService.queryOrderStatus入参：" + paramJson);
		String resultJson = commonApiRequestService.wrapRequest(this.getUrlByProductAndType(productId, ProductApiEnum.QUERY_ORDER_STATUS.getType()), paramJson, false);
		LOG.info("------------AfterCreditService.queryOrderStatus出参：" + resultJson);
		
		CommonResultVo<Map<String,Integer>> commonResult = JacksonUtil.Json2Obj(resultJson, new TypeReference<CommonResultVo<Map<String,Integer>>>(){});
		if(!commonResult.isSuccess()) {
			throw new BaseException(commonResult.getMsg());
		}
		Integer orderStatus = commonResult.getData().get("order_status");
		
		// 更新申请记录表中的审批状态
		custProductApply.setAuditResult(LoanOrderStatusEnum.getAuditResultByOrderStatus(orderStatus));
		custProductApplyService.update(custProductApply);
		
		// 返回数据
		ProductOrderLog param = new ProductOrderLog();
		param.setCustId(cust.getCustId());
		param.setOrderId(custProductApply.getOrderId());
		param.setProductId(productId);
		
		ProductOrderLog productOrderLog = productOrderLogService.find(param).get(0);
		String bankCard = productOrderLog.getBankCard();
		BankInfo bankInfo = bankInfoService.findAllForMap().get(productOrderLog.getBankCode());
		return new QueryOrderStatusResultDto(Float.valueOf(productOrderLog.getLoanAmount() + ""), 
				productOrderLog.getLoanTerm(), 
				LoanTermUnitEnum.getNameByType(custProductApply.getLoanTermType()),
				bankCard.substring(bankCard.length() - 4), 
				productOrderLog.getBankCode(), bankInfo.getBankName(), 
				bankInfo.getBankIcon(), orderStatus);
	}

	@Log
	public LoanBindCardListDto loanbindCardList(Long productId, Cust cust) {
		List<ProductLoanBindCard> list = productLoanBindCardService.loanbindCardList(cust.getCustId(),productId);
		List<LoanBindCardDto> loanBindCardList = new ArrayList<LoanBindCardDto>();
		if(Utility.isNullOrEmpty(list)) {
			return new LoanBindCardListDto(loanBindCardList);
		}
		Map<String, BankInfo> allBankInfo = bankInfoService.findAllForMap();
		for(int i = 0; i < list.size(); i++) {
			LoanBindCardDto loanBindCardDto = new LoanBindCardDto();
			if(i == 0) {
				loanBindCardDto.setIsDefault(true);
			}
			loanBindCardDto.setBankCard(list.get(i).getBankCard());
			loanBindCardDto.setBankCode(list.get(i).getBankCode());
			loanBindCardDto.setBankName(list.get(i).getBankName());
			loanBindCardDto.setMobile(list.get(i).getMobile());
			loanBindCardDto.setBankIcon(allBankInfo.get(loanBindCardDto.getBankCode()).getBankIcon());
			
			loanBindCardList.add(loanBindCardDto);
		}
		return new LoanBindCardListDto(loanBindCardList);
	}

	@Log
	public Boolean submitBindCard(Long productId, SubmitBindCardParamDto param,
			Cust cust) throws Exception {
		// 校验参数
		ProductLoanBindCard productLoanBindCard = new ProductLoanBindCard();
		productLoanBindCard.setCustId(cust.getCustId());
		productLoanBindCard.setProductId(productId);
		productLoanBindCard.setBankCard(param.getBankCard());
		
		List<ProductLoanBindCard> list = productLoanBindCardService.find(productLoanBindCard);
		if(Utility.isNullOrEmpty(list)) {
			throw new BaseException("参数不正确");
		}
		productLoanBindCard = list.get(0);
		
		if(productLoanBindCard.getStatus() == 1) {// 已经绑卡成功了
			return false;
		}
		
		Map<String,String> paramMap = new HashMap<String,String>();
		paramMap.put("user_id", CUSTID_PREFIX + cust.getCustId());
		paramMap.put("bank_code", param.getBankCode());
		paramMap.put("bank_card", param.getBankCard());
		paramMap.put("mobile", param.getMobile());
		if(StringUtils.isNotBlank(param.getVercode())) {
			paramMap.put("vercode", param.getVercode());
		}
		String paramJson = JacksonUtil.Obj2Json(paramMap);
		LOG.info("------------AfterCreditService.submitBindCard入参：" + paramJson);
		String resultJson = commonApiRequestService.wrapRequest(this.getUrlByProductAndType(productId, ProductApiEnum.BIND_BANKCARD.getType()), paramJson, false);
		LOG.info("------------AfterCreditService.submitBindCard出参：" + resultJson);
		
		CommonResultVo<Map<String,String>> commonResult = JacksonUtil.Json2Obj(resultJson, new TypeReference<CommonResultVo<Map<String,String>>>(){});
		if(commonResult.isSuccess()) {
			// 成功绑定需要将绑定记录状态设置为1
			productLoanBindCard.setStatus(1);
			productLoanBindCardService.update(productLoanBindCard);
			return false;
		} else if(commonResult.getCode() == 2) {
			return true;
		} else {
			throw new BaseException(commonResult.getMsg());
		}
	}

	@Log
	public LoanOrderIndexDto loanOrderIndex(Long productId,Cust cust) {
		CustProductApply custProductApply = custProductApplyService.findByCustAndProduct(cust.getCustId(), productId);
		List<Integer> peroidList = new ArrayList<Integer>();
		for(String str : custProductApply.getLoanTerm().split(",")) {
			peroidList.add(Integer.parseInt(str.trim()));
		}
		return new LoanOrderIndexDto(custProductApply.getMinAmount() / 100, 
				custProductApply.getMaxAmount() / 100, peroidList, 
				LoanTermUnitEnum.getNameByType(custProductApply.getLoanTermType()),
				custProductApply.getRangeAmount() / 100);
	}

	@Log
	public LoanBindCardListDto bankInfoList() {
		LoanBindCardListDto result = new LoanBindCardListDto();
		List<LoanBindCardDto> list = new ArrayList<LoanBindCardDto>();
		result.setList(list);
		Map<String, BankInfo> allBankInfo = bankInfoService.findAllForMap();
		for (Entry<String,BankInfo> entry : allBankInfo.entrySet()) {
			BankInfo bankInfo = entry.getValue();
			LoanBindCardDto loanBindCardDto = new LoanBindCardDto();
			loanBindCardDto.setBankName(bankInfo.getBankName());
			loanBindCardDto.setBankCode(bankInfo.getBankCode());
			loanBindCardDto.setBankIcon(bankInfo.getBankIcon());
			list.add(loanBindCardDto);
		}
		return result;
	}

	@Log
	public QueryBankByCardResultDto queryBankByCard(String bankCard) throws Exception {
		CustCardDto queryCardBin = custCardService.queryCardBin(bankCard);
		String bankCode = queryCardBin.getBankCode();
		String bankName = queryCardBin.getBankName();
		Integer cardType = queryCardBin.getCardType().getCode();
		String bankIcon = bankInfoService.findAllForMap().get(bankCode.toUpperCase()).getBankIcon();
		
		QueryBankByCardResultDto result = new QueryBankByCardResultDto();
		result.setBankCode(bankCode);
		result.setBankName(bankName);
		result.setBankIcon(bankIcon);
		result.setCardType(cardType);
		return result;
	}

	@Log
	public Cust queryCustInfo(Cust cust) throws BaseException {
		AuthIdentity authIdentity = authIdentityService.findByProcessId(cust.getLastProccessId());
		if(null == authIdentity) {
			throw new BaseException("用户未高实");
		}
		cust.setCustIdno(authIdentity.getCitizenId());
		cust.setCustName(authIdentity.getName());
		return cust;
	}

	@Log
	public String queryJump(Cust cust, Long productId) throws Exception {
		ProcessSpecial processSpecial = processSpecialService.findByProductId(productId);
		if(null == processSpecial) {
			return null;
		}
		String[] afterCreditJumpArr = processSpecial.getAfterCreditJump().split(",");
		if(!processSpecial.getJoinLoanApi()) {
			return afterCreditJumpArr[0].trim();
		}
		
		// 如果是闪贷，并且没有提现过，则需要预检，如果预检不通过跳转到失败页面
		if(productId == 3) {
			CustProductApply custProductApply = custProductApplyService.findByCustAndProduct(cust.getCustId(), productId);
			if(custProductApply.getAuditResult().intValue() == ProductAuditResultEnum.ApplySuccess.getCode()) {
				CheckOrderResultDto checkUser = sdAfterCreditService.checkUser(productId, cust);
				if(!checkUser.getCanLoanFlag()) {
					return afterCreditJumpArr[2];
				}
			}
		}
		
		CustProductApply custProductApply = custProductApplyService.findByCustAndProduct(cust.getCustId(), productId);
		Integer auditResult = custProductApply.getAuditResult();
		int loanApplyingCode = ProductAuditResultEnum.LoanApplying.getCode().intValue(); // 放款申请审核中
		int speedloanCommittingCode = ProductAuditResultEnum.SpeedloanCommitting.getCode().intValue(); // 快贷流程爬取中
		
		// auditResult >= 8 并且 != 22， 状态需要在放贷订单已提交，显示放款申请结果页，否则显示放款首页
		boolean flag = (auditResult >= loanApplyingCode && auditResult != speedloanCommittingCode) ? true : false;
		if(flag) { // 跳转到放贷审批结果页
			return afterCreditJumpArr[1].trim();
		}
		// 跳转到放贷首页
		return afterCreditJumpArr[0].trim();
	}

	@Log
	public void updateLoanOrderStatus(Long productId, String orderId, Integer orderStatus) throws BaseException {
		CustProductApply condition = new CustProductApply();
		if(null == productId || null == orderId) {
			throw new BaseException("参数错误");
		}
		condition.setProductId(productId);
		condition.setOrderId(orderId);
		List<CustProductApply> list = custProductApplyService.findByConditions(condition);
		if(Utility.isNullOrEmpty(list)) {
			throw new BaseException("未找到放款申请记录");
		}
		CustProductApply  custProductApply = list.get(0);
		Integer auditResult = LoanOrderStatusEnum.getAuditResultByOrderStatus(orderStatus);
		custProductApply.setAuditResult(auditResult);
		custProductApplyService.update(custProductApply);
	}

	@Log
	public void uploadSignature(Cust cust, Long productId, String signatureImg) throws BaseException {
		CustProductApply custProductApply = custProductApplyService.findByCustAndProduct(cust.getCustId(), productId);
		if(null == custProductApply) {
			throw new BaseException("参数错误");
		}
		// 上传签名图片
		ResourceDto upload = ftpResourceService.upload(ResourceTypeEnum.IMG, "loanSignature.jpg", Base64.decodeBase64(signatureImg));
		custProductApply.setSignature(upload.getResourceCode());
		custProductApplyService.update(custProductApply);
	}
	
	@Log
	public String  sdGetDeviceId(Cust cust) throws BaseException {
		return  custProductApplyService.sdGetDeviceID(cust.getCustId());		
	}
	
	@Log
	public String  sdGetCreditAmt(Cust cust,Long productId) throws BaseException {
		return  custProductApplyService.sdGetCreditAmt(cust.getCustId(),productId);		
	}
	
	
}
